package com.msmall.community.tag;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.jsp.JspException;
import javax.servlet.jsp.JspWriter;
import javax.servlet.jsp.tagext.TagSupport;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.msmall.base.util.ConstantDB;
import com.msmall.base.util.ConstantUrl;
import com.msmall.base.util.Constants;
import com.msmall.community.ApplicationManager;
import com.msmall.community.common.OnlineUser;
import com.msmall.community.user.model.UserInfo;
import com.msmall.community.user.model.UserProfile;
import com.msmall.community.user.service.UserManager;

public class UserInfoTag extends TagSupport{

	private static final Log logger = LogFactory.getLog(UserInfoTag.class);
	
	private static final long serialVersionUID = 5735728545974742340L;

	private String type; // nickname gnickname avatar score bbstopic bbsthread bbselite
	
	private Long userid;
	
	private String nickname;
	
	private boolean link = true; // 是否有链接
	
	private String style; //样式
	
	public int doStartTag() throws JspException {
		return (SKIP_BODY);
	}

	public int doEndTag() throws JspException {
		JspWriter writer = pageContext.getOut();
		String ctx = ((HttpServletRequest)pageContext.getRequest()).getContextPath();
		String info = "";
		try{
			if(null == style || "".equals(style))
				style = "a2";
			if("nickname".equalsIgnoreCase(type)){ // nickname是传递过来的
				if(Constants.USER_ID_START.compareTo(userid) > 0){ //匿名用户名，不产生链接
					link = false;
				}
				if(link){
					if("wml".equalsIgnoreCase(getUserAgentType())){
						info = "<a href=\"" + ctx + ConstantUrl.WML_USERINFO + "?usr=" + userid + "\">" + nickname + "</a>";
					}else{
						info = "<a class=\"" + style + "\" href=\"" + ctx + ConstantUrl.HTML_USERINFO + "?usr=" + userid + "\">" + nickname + "</a>";
					}
					
				}else{
					info = nickname;
				}
			}
			if("gnickname".equalsIgnoreCase(type)){ // 通过用户名获得昵称等信息
				if(Constants.USER_ID_START.compareTo(userid) > 0){ //匿名用户名，不产生链接
					link = false;
				}
				nickname = getNickname(userid);
				if(link){
					if("wml".equalsIgnoreCase(getUserAgentType())){
						info = "<a href=\"" + ctx + ConstantUrl.WML_USERINFO + "?usr=" + userid + "\">" + nickname + "</a>";
					}else{
						info = "<a class=\"" + style + "\" href=\"" + ctx + ConstantUrl.HTML_USERINFO + "?usr=" + userid + "\">" + nickname + "</a>";
					}
					
				}else{
					info = nickname;
				}
			}
			if("avatar".equalsIgnoreCase(type)){
				info = getAvatar(userid);
			}
			if("score".equalsIgnoreCase(type)){
				info = getScore(userid);
			}
			if("bbstopic".equalsIgnoreCase(type)){
				UserProfile profile = getUserProfile(userid);
				info = String.valueOf(profile.getTopicAmount());
			}
			if("bbsthread".equalsIgnoreCase(type)){
				UserProfile profile = getUserProfile(userid);
				info = String.valueOf(profile.getThreadAmount());
			}
			if("bbselite".equalsIgnoreCase(type)){
				UserProfile profile = getUserProfile(userid);
				info = String.valueOf(profile.getEliteAmount());
			}
			writer.print(info);
		}catch(Exception e){
			logger.debug(e);
		}
		return (SKIP_BODY);
	}
	
	private UserProfile getUserProfile(Long userid){
		UserManager userManager = (UserManager)ApplicationManager.appContext.getBean("userManager");
		return userManager.getProfileByUserid(userid);
	}
	private String getAvatar(Long userid){
		UserManager userManager = (UserManager)ApplicationManager.appContext.getBean("userManager");
		UserInfo info = userManager.getByUserid(userid);
		if(info != null)
			return info.getAvatar();
		return ConstantDB.AVATAR_DEFAULT;
	}
	private String getScore(Long userid){
		UserManager userManager = (UserManager)ApplicationManager.appContext.getBean("userManager");
		UserInfo info = userManager.getByUserid(userid);
		if(info != null)
			return String.valueOf(info.getScore().getScore());
		return "0";
	}
	private String getNickname(Long userid){
		String nickname = "";
		UserManager userManager = (UserManager)ApplicationManager.appContext.getBean("userManager");
		UserInfo info = userManager.getByUserid(userid);
		if(info != null)
			nickname = info.getNickname();
		return nickname;
	}
	private String getUserAgentType(){
		try{
			return OnlineUser.getOnlineUsers().get(getUserid()).getUserAgentBean().getUserAgentType();
		}catch(Exception e){
			return Constants.USER_AGENT_TYPE_HTML;
		}
	}
	
	public String getType() {
		return type;
	}

	public void setType(String type) {
		this.type = type;
	}

	public String getNickname() {
		return nickname;
	}

	public void setNickname(String nickname) {
		this.nickname = nickname;
	}

	public boolean getLink() {
		return link;
	}

	public void setLink(boolean link) {
		this.link = link;
	}

	public String getStyle() {
		return style;
	}

	public void setStyle(String style) {
		this.style = style;
	}

	public Long getUserid() {
		return userid;
	}

	public void setUserid(Long userid) {
		this.userid = userid;
	}
}
